package com.hardware.service.impl.category;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hardware.common.CustomException;
import com.hardware.common.R;
import com.hardware.entity.Customer;
import com.hardware.entity.Shop;
import com.hardware.entity.category.CustomerCategory;
import com.hardware.entity.category.SupplierCategory;
import com.hardware.mapper.Category.CustomerCategoryMapper;
import com.hardware.mapper.Category.SupplierCategoryMapper;
import com.hardware.service.CustomerService;
import com.hardware.service.ShopService;
import com.hardware.service.category.CustomerCategoryService;
import com.hardware.service.category.SupplierCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerCategoryServiceImpl extends ServiceImpl<CustomerCategoryMapper, CustomerCategory> implements CustomerCategoryService {
    @Autowired
    private CustomerService customerService;
    /**
     * 根据id删除分类，删除之前需要进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Customer> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        dishLambdaQueryWrapper.eq(Customer::getCategoryId,id);
        int count = customerService.count(dishLambdaQueryWrapper);

        //查询当前分类是否关联了商品，如果已经关联，抛出一个业务异常
        if(count > 0){
            //已经关联商品，抛出一个业务异常
            throw new CustomException("当前分类下关联了商品，不能删除");
        }
        //正常删除分类
        super.removeById(id);
    }

    @Override
    public R<Page> page(int page, int pageSize) {
        //分页构造器
        Page<CustomerCategory> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<CustomerCategory> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
        queryWrapper.orderByAsc(CustomerCategory::getSort);
        //分页查询
        page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }




}
